package org.ruoyi.interview.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.ruoyi.common.core.utils.MapstructUtils;
import org.ruoyi.common.mybatis.core.page.PageQuery;
import org.ruoyi.common.mybatis.core.page.TableDataInfo;
import org.ruoyi.interview.domain.InterviewQuestionMastered;
import org.ruoyi.interview.domain.bo.InterviewQuestionMasteredBo;
import org.ruoyi.interview.domain.vo.InterviewQuestionFavoriteCategoryVo;
import org.ruoyi.interview.domain.vo.InterviewQuestionMasteredCategoryVo;
import org.ruoyi.interview.domain.vo.InterviewQuestionMasteredVo;
import org.ruoyi.interview.mapper.InterviewQuestionMasteredMapper;
import org.ruoyi.interview.service.IInterviewQuestionMasteredService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 面试题掌握Service业务层处理
 *
 * @author ruoyi
 */
@Service
@RequiredArgsConstructor
public class InterviewQuestionMasteredServiceImpl implements IInterviewQuestionMasteredService {

    private final InterviewQuestionMasteredMapper baseMapper;

    /**
     * 查询面试题掌握
     *
     * @param masteredId 掌握主键
     * @return 面试题掌握
     */
    @Override
    public InterviewQuestionMasteredVo selectInterviewQuestionMasteredById(Long masteredId) {
        return baseMapper.selectVoById(masteredId);
    }

    /**
     * 查询面试题掌握列表
     *
     * @param bo 面试题掌握业务对象
     * @return 面试题掌握集合
     */
    @Override
    public List<InterviewQuestionMasteredVo> selectInterviewQuestionMasteredList(InterviewQuestionMasteredBo bo) {
        LambdaQueryWrapper<InterviewQuestionMastered> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 查询面试题掌握列表
     *
     * @param bo 面试题掌握业务对象
     * @param pageQuery 分页参数
     * @return 面试题掌握集合
     */
    @Override
    public TableDataInfo<InterviewQuestionMasteredVo> selectInterviewQuestionMasteredList(InterviewQuestionMasteredBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<InterviewQuestionMastered> lqw = buildQueryWrapper(bo);
        Page<InterviewQuestionMastered> page = baseMapper.selectPage(pageQuery.build(), lqw);
        List<InterviewQuestionMasteredVo> vos = MapstructUtils.convert(page.getRecords(), InterviewQuestionMasteredVo.class);
        Page<InterviewQuestionMasteredVo> voPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        voPage.setRecords(vos);
        return TableDataInfo.build(voPage);
    }

    /**
     * 查询面试题掌握列表及题目详情
     *
     * @param bo 面试题掌握业务对象
     * @param pageQuery 分页参数
     * @return 面试题掌握集合
     */
    @Override
    public TableDataInfo<InterviewQuestionMasteredCategoryVo> selectInterviewQuestionMasteredQuestionList(InterviewQuestionMasteredBo bo, PageQuery pageQuery) {
        // 使用链表SQL语句直接查询掌握及题目详情
        com.baomidou.mybatisplus.core.metadata.IPage<InterviewQuestionMasteredCategoryVo> page = baseMapper.selectMasteredQuestionWithDetailPage(pageQuery.build(), bo);
        return TableDataInfo.build(page);
    }

    /**
     * 新增面试题掌握
     *
     * @param bo 面试题掌握业务对象
     * @return 结果
     */
    @Override
    public Boolean insertInterviewQuestionMastered(InterviewQuestionMasteredBo bo) {
        InterviewQuestionMastered add = MapstructUtils.convert(bo, InterviewQuestionMastered.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setMasteredId(add.getMasteredId());
        }
        return flag;
    }

    /**
     * 批量删除面试题掌握
     *
     * @param questionId 需要删除的题目ID
     * @return 结果
     */
    @Override
    public Boolean deleteInterviewQuestionMasteredByQuestionId(Long questionId) {
        LambdaQueryWrapper<InterviewQuestionMastered> lqw = Wrappers.lambdaQuery();
        lqw.eq(InterviewQuestionMastered::getQuestionId, questionId);
        return baseMapper.delete(lqw) > 0;
    }

    /**
     * 根据用户ID和题目ID查询掌握记录
     *
     * @param userId 用户ID
     * @param questionId 题目ID
     * @return 掌握记录
     */
    @Override
    public InterviewQuestionMasteredVo selectInterviewQuestionMasteredByUserIdAndQuestionId(Long userId, Long questionId) {
        LambdaQueryWrapper<InterviewQuestionMastered> lqw = Wrappers.lambdaQuery();
        lqw.eq(InterviewQuestionMastered::getUserId, userId);
        lqw.eq(InterviewQuestionMastered::getQuestionId, questionId);
        return baseMapper.selectVoOne(lqw);
    }

    /**
     * 构建查询对象
     *
     * @param bo 面试题掌握业务对象
     * @return 查询对象
     */
    private LambdaQueryWrapper<InterviewQuestionMastered> buildQueryWrapper(InterviewQuestionMasteredBo bo) {
        LambdaQueryWrapper<InterviewQuestionMastered> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, InterviewQuestionMastered::getUserId, bo.getUserId());
        lqw.eq(bo.getQuestionId() != null, InterviewQuestionMastered::getQuestionId, bo.getQuestionId());
        lqw.eq(InterviewQuestionMastered::getDelFlag, "0");
        return lqw;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(InterviewQuestionMastered entity) {
        // TODO 根据业务逻辑添加校验
    }
}
